package com.ssuu.ssuusnow.project.manage.menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssuu.ssuusnow.model.result.ResultVo;
import com.ssuu.ssuusnow.project.manage.menu.entity.Menu;
import com.ssuu.ssuusnow.project.manage.menu.mapper.MenuMapper;
import com.ssuu.ssuusnow.project.manage.menu.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssuu.ssuusnow.project.manage.menu_role.entity.MenuRole;
import com.ssuu.ssuusnow.project.manage.menu_role.mapper.MenuRoleMapper;
import com.ssuu.ssuusnow.project.manage.menu_role.service.impl.MenuRoleServiceImpl;
import com.ssuu.ssuusnow.project.manage.user.entity.User;
import com.ssuu.ssuusnow.project.manage.user_role.service.impl.UserRoleServiceImpl;
import com.ssuu.ssuusnow.utils.security.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author sun
 * @since 2023-06-14
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    MenuMapper menuMapper;
    @Resource
    UserRoleServiceImpl userRoleService;
    @Resource
    MenuRoleServiceImpl menuRoleService;

    @Override
    public List<Menu> menuList(List<MenuRole> menuRoles, Integer type) {
        QueryWrapper<Menu> wrappers = new QueryWrapper<>();
        List<Long> listLong = new ArrayList<>();
        menuRoles.forEach(x -> listLong.add(x.getMenuID()));
        wrappers.and(x -> {
            x.eq("isdel", false);
            if (type == 1) {
                x.ne("menuType", 1669181114641731586L);
            } else {
                x.eq("menuType", 1669181114641731586L);
            }
            x.in("id", listLong);
        });
        List<Menu> menus = menuMapper.selectList(wrappers);
        return menus;
    }

    @Override
    public List<Menu> menusList(Integer type) {
        QueryWrapper<Menu> wrappers = new QueryWrapper<>();
        wrappers.and(x -> {
            if (type == 1) {
                x.ne("menuType", 1669181114641731586L);
            }
            if (type == 3) {
                x.eq("menuType", 1669181114641731586L);
            }
            x.eq("isdel", false);
        });
        List<Menu> menus = menuMapper.selectList(wrappers);
        return menus;
    }

    @Override
    public List<String> menuAll(User user) {
        List<String> Pers = new ArrayList<>();
        List<Menu> menus = new ArrayList<>();
        if (user.getUserType().equals(1669180669244395521L)) {
            menus = menusList(1);
            menus.forEach(x -> Pers.add(x.getPerms()));
            return Pers;
        }
        menus = menuList(menuRoleService.getlist(userRoleService.getList(user.getId())), 1);
        menus.forEach(x -> Pers.add(x.getPerms()));
        return Pers;
    }

    @Override
    public List<Menu> menuAllCD(User user, Integer type) {
        if (user.getUserType().equals(1669180669244395521L)) {
            return menusList(type);
        }
        return menuList(menuRoleService.getlist(userRoleService.getList(user.getId())), type);
    }

    @Override
    public Object listTo(Menu t) {
        QueryWrapper<Menu> wrappers = new QueryWrapper<>();
        wrappers.orderByDesc("sort");
        wrappers.and(wrapper -> {
            wrapper.eq("isdel", false);
            wrapper.like("menuname", Objects.isNull(t.getMenuname()) ? "" : t.getMenuname());
            if (!Objects.isNull(t.getMenuType())) {
                wrapper.eq("menuType", t.getMenuType());
            }
//        wrapper.eq("status",Objects.isNull(t.getStatus())?"":t.getStatus());
        });
        Page<Menu> page = new Page<>(t.getCurrentPage(),t.getPageSize());
        Object userPage = t.getPageSize() > 0 && t.getCurrentPage() > 0 ? menuMapper.selectPage(page, wrappers) : menuMapper.selectList(wrappers);
        return userPage;
    }

    @Override
    public int AddTo(Menu t) {
        t.setCreateTime(LocalDateTime.now());
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        return menuMapper.insert(t);
    }

    @Override
    public int UpdateTo(Menu t) {
        t.setUpdateTime(LocalDateTime.now());
        User lou = SecurityUtils.LoginUsers();
        t.setUpdateby(lou.getId());
        return menuMapper.updateById(t);
    }

    @Override
    public int DelTo(Long t) {
        Menu u = new Menu();
        u.setId(t);
        u.setUpdateTime(LocalDateTime.now());
        u.setIsdel(true);
        User lou = SecurityUtils.LoginUsers();
        u.setUpdateby(lou.getId());
        return menuMapper.deleteById(u);
    }
}
